package com.yj.consumer.service;

import com.yj.consumer.config.CommonFeignConfig;
import com.yj.pojo.Payment;
import com.yj.pojo.Result;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@FeignClient(value = "microservice-provider", path = "/payment", configuration = {FeignConfiguration.class}, fallback = PaymentServiceFallback.class)
public interface PaymentService {

    @GetMapping("/create")
    Result<Payment> create(Payment payment);

    @GetMapping("/get/{id}")
    Result<Payment> queryById(@PathVariable("id") Long id);

    @GetMapping("/timeout")
    String paymentTimeOut() throws InterruptedException;

    @GetMapping("/error")
    String paymentError() throws InterruptedException;
}
/**
 * FeignClient配置类
 */
class FeignConfiguration {
    @Bean
    public PaymentServiceFallback fallback() {
        return new PaymentServiceFallback();
    }
}
/**
 * FeignClient降级方法
 */
class PaymentServiceFallback implements PaymentService {
    @Override
    public Result<Payment> create(Payment payment) {
        return new Result<>(500, "/payment/create已降级（限流、熔断、异常）");
    }
    @Override
    public Result<Payment> queryById(Long id) {
        return new Result<>(500, "/payment/get/{id}已降级（限流、熔断、异常）");
    }
    @Override
    public String paymentTimeOut() throws InterruptedException {
        return "/payment/timeout已降级（限流、熔断、异常）";
    }
    @Override
    public String paymentError() throws InterruptedException {
        return "/payment/error已降级（限流、熔断、异常）";
    }
}